﻿using JetBrains.Annotations;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using WMS.Seven.Models;
using WMS.Seven.zhangduo.Dto;

namespace WMS.Seven.zhangduo.ReturnGoods
{
    public class ReturnGoodsService:ApplicationService,IReturnGoodsAppService
    {
        public readonly IRepository<Sales,int> _Salesrepository;
        public readonly IRepository<SaleDetils, int> _SaleDetilsrepository;
        public readonly IRepository<Products, int> _Productsrepository;
        public ReturnGoodsService(IRepository<Sales, int> salesrepository, IRepository<SaleDetils, int> saleDetilsrepository, IRepository<Products, int> productsrepository)
        {
            _Salesrepository = salesrepository;
            _SaleDetilsrepository = saleDetilsrepository;
            _Productsrepository = productsrepository;
        }
        /// <summary>
        /// 显示退货信息
        /// </summary>
        /// <param name="SalesOrderCode">退货单号</param>
        /// <param name="ProductCode">销售单号</param>
        /// <param name="ProductName">产品单号</param>
        /// <returns>返回退货信息列表</returns>
        
        public async Task<string> GetReturnService(string? SalesOrderCode, string? ProductCode, string? ProductName)
        {
            var item=(from sales in await _Salesrepository.GetListAsync()
                      join salesdetils in await _SaleDetilsrepository.GetListAsync()
                      on sales.SalesOrderCode equals salesdetils.SalesOrderCode
                      join products in await _Productsrepository.GetListAsync()
                      on salesdetils.ProductCode equals products.ProductCode
                      where products.Category=="退货"
                      select new ReturnGoodsDto()
                      {
                          Id=sales.Id,
                          SalesOrderCode= sales.SalesOrderCode,
                          ProductName= products.ProductName,
                          ProductCode= products.ProductCode,
                          ProductSpecification = products.ProductSpecification,
                          Category= products.Category,
                          StorageUnit= products.StorageUnit,
                          Unit= products.Unit,
                          ReturnQuantity=sales.ReturnQuantity,
                          UnitPrice = products.UnitPrice,
                          CustomerName=sales.CustomerName
                      }).Distinct().ToList();
            if(!string.IsNullOrEmpty(SalesOrderCode))
            {
                item = item.Where(c => c.SalesOrderCode.Contains(SalesOrderCode)).ToList();
            }
            if(!string.IsNullOrEmpty(ProductCode))
            {
                item = item.Where(c => c.ProductCode.Contains(ProductCode)).ToList();
            }
            if(!string.IsNullOrEmpty(ProductName))
            {
                item = item.Where(c => c.ProductName.Contains(ProductName)).ToList();
            }

            return JsonConvert.SerializeObject(new PageDto<ReturnGoodsDto> { code = "", data = item, msg = "", count = item.Count });
        }
        /// <summary>
        /// 显示单个退货信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回退货信息实体</returns>
        public async Task<ReturnGoodsDto> GetReturnGood(int id)
        {
            var item = (from sales in await _Salesrepository.GetListAsync()
                        join salesdetils in await _SaleDetilsrepository.GetListAsync()
                        on sales.SalesOrderCode equals salesdetils.SalesOrderCode
                        join products in await _Productsrepository.GetListAsync()
                        on salesdetils.ProductCode equals products.ProductCode
                        where sales.Id == id
                        select new ReturnGoodsDto()
                        {
                            Id = sales.Id,
                            SalesOrderCode = sales.SalesOrderCode,
                            ProductName = products.ProductName,
                            ProductCode = products.ProductCode,
                            ProductSpecification = products.ProductSpecification,
                            Category = products.Category,
                            StorageUnit = products.StorageUnit,
                            Unit = products.Unit,
                            ReturnQuantity = sales.ReturnQuantity,
                            UnitPrice = products.UnitPrice,
                            CustomerName = sales.CustomerName
                        });
            return (ReturnGoodsDto)item;
        }
        /// <summary>
        /// 删除退货信息
        /// </summary>
        /// <param name="id">实体id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DelReturnGood(int id)
        {
            var item=await _Salesrepository.GetAsync(c=>c.Id==id);
             await _Salesrepository.DeleteAsync(item);
            return 1;
        }
    }
}
